package com.eascs.ticket.ticket.controller;

import java.text.Format;

import javax.inject.Inject;

import org.springframework.stereotype.Component;

import com.eascs.app.context.ConfigContext;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.dao.sql.ObjectSqlBuilder;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.ticket.common.controller.BaseMasterViewController;
import com.eascs.ticket.ticket.form.TicketSearchForm;
import com.eascs.ticket.ticket.format.DueDateViewFormat;
import com.eascs.ticket.ticket.format.TitleViewFormat;
import com.eascs.ticket.ticket.service.TicketSearchService;
import com.eascs.ticket.ticket.vo.GroupByHelper;
import com.eascs.ticket.ticket.vo.OrderByConstant;
import com.eascs.ticket.ticket.vo.OrderByHelper;
import com.eascs.web.context.RequestContext;
import com.eascs.web.sso.UserHolder;

@Component
public class TicketSearchComponent extends BaseMasterViewController {

	@Inject
	private TicketSearchService ticketSearchService;

	public PaginationInfo search(TicketSearchForm ticketSearchForm) {
		if (StringUtil.isEmpty(ticketSearchForm.getOrderBy())) {
			ticketSearchForm.setOrderBy(OrderByConstant.ORDER_BY_VERSION);
		}
		PageResult<Object[]> pageResult = searchTicket(ticketSearchForm);
		PaginationInfo paginationInfo = buildPaginationInfo(ticketSearchForm, pageResult);

		if (StringUtil.hasText(ticketSearchForm.getGroupBy())) {
			paginationInfo.setTitles(new String[] { "分组名", "统计数" });
			paginationInfo.setFirstRenderColumnIndex(0);
			paginationInfo.setTitleButtons(null);
		}

		return paginationInfo;
	}

	public PaginationInfo getMyWatcher(TicketSearchForm ticketSearchForm) {

		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, ticketSearchForm.getPageSize(), ticketSearchForm.getPageIndex());
		sqlPageQuery.setQuerySql(ConfigContext.getStringValue("sql.ticket.search") + " and t.id in (select tw.ticket_id from ticket_watcher tw where tw.watcher_user_code=?) order by t.create_date desc");
		sqlPageQuery.addParam(UserHolder.getUser().getCode());
		PageResult<Object[]> pageResult = this.ticketSearchService.searchTicket(sqlPageQuery);

		return buildPaginationInfo(ticketSearchForm, pageResult);
	}

	public PaginationInfo getMyDone(TicketSearchForm ticketSearchForm) {

		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, ticketSearchForm.getPageSize(), ticketSearchForm.getPageIndex());
		sqlPageQuery.setQuerySql("select t.* from (" + ConfigContext.getStringValue("sql.ticket.search") + ") t left join  ticket_time_log ttl on  t.id = ttl.ticket_id where  ttl.operator=? order by end_time desc,start_time desc");
		sqlPageQuery.addParam(UserHolder.getUser().getCode());
		PageResult<Object[]> pageResult = this.ticketSearchService.searchTicket(sqlPageQuery);

		return buildPaginationInfo(ticketSearchForm, pageResult);
	}

	public PaginationInfo buildPaginationInfo(TicketSearchForm ticketSearchForm, PageResult<Object[]> pageResult) {
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, new String[] { "项目", "类型", "编号", "标题", "状态", "版本", "验收日期", "经办", "报告人" });
		paginationInfo.setTitleDescriptions(new String[] { null, null, null, null, null, null, "验收日期根据不同阶段日期可能会不一样", null, null });
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setUrl(UrlUtil.resetParam(RequestContext.getRequestUrlWithQueryString(), "pageIndex", ""));
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setTitleButtons(new Button[] { null, null, ButtonHelper.createOpenButton(null, "/ticket/view/"), null, null, ButtonHelper.createOpenButton(null, "/ticket/view/{10}?fromTicketId=") });
		paginationInfo.setRendTo(ticketSearchForm.getRender());
		paginationInfo.setFormats(new Format[] { null, null, null, new TitleViewFormat(), null, null, new DueDateViewFormat() });
		return paginationInfo;
	}

	public PageResult<Object[]> searchTicket(TicketSearchForm ticketSearchForm) {
		return this.searchTicket(ticketSearchForm, "sql.ticket.search");
	}

	public PageResult<Object[]> searchTicket(TicketSearchForm ticketSearchForm, String sqlKey) {
		buildOrderInfo(ticketSearchForm);
		buildGroupInfo(ticketSearchForm);
		ObjectSqlBuilder objectSqlBuilder = ObjectSqlBuilder.create(ticketSearchForm, ConfigContext.getStringValue(sqlKey));
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, ticketSearchForm.getPageSize(), ticketSearchForm.getPageIndex());
		sqlPageQuery.setQuerySql(objectSqlBuilder.getSql());
		sqlPageQuery.addParams(objectSqlBuilder.getParams());
		return this.ticketSearchService.searchTicket(sqlPageQuery);
	}

	public void buildOrderInfo(TicketSearchForm ticketSearchForm) {
		String orderBy = ticketSearchForm.getOrderBy();
		if (StringUtil.hasText(orderBy)) {
			ticketSearchForm.setSqlOrder(OrderByHelper.getOrderExpression(orderBy, orderBy));
		}
	}

	public void buildGroupInfo(TicketSearchForm ticketSearchForm) {
		String groupBy = ticketSearchForm.getGroupBy();
		if (StringUtil.hasText(groupBy)) {
			ticketSearchForm.setSqlGroup(GroupByHelper.getGroupExpression(groupBy));
		}
	}

}
